package org.beynet.docs.format;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Link {

    public Link(URI uri,String rel,String type) {
        this.uri = uri ;
        this.rel = rel;
        this.type = type ;
    }
    
    public URI getURI(){
        return(uri);
    }
    public String getType(){
        return(type);
    }
    public String getRel(){
        return(rel);
    }
    
    /**
     * read a NodeList containing Links an add each link found into the provided list
     * @param links
     * @param linksNodes
     */
    public static void makeFromNodeList(List<Link> links,NodeList linksNodes) {
        for (int i=0;i<linksNodes.getLength();i++){
            Element el = (Element)linksNodes.item(i);
            String rel = el.getAttribute("rel");
            String type = el.getAttribute("type");
            String href = el.getAttribute("href");
            try {
                Link l = new Link(new URI(href), rel, type);
                links.add(l);
            } catch (URISyntaxException e) {
               // skipping this link with an invalid URI
                logger.warn("Found an invalid link into link list");
            }
        }
    }

    private URI uri;
    private String rel;
    private String type;
    
    private final static Logger logger  = Logger.getLogger(DocumentListFeed.class);
}
